<template>
  <div>
    <my-breadcrumb :items="['社区管理', '自主活动']"/>
    <search :formItems="searchItems" @search="getList" ref="search"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">新增活动</el-button>
    </div>
    <el-table :data="activities" @sort-change="orderByTime">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="创建时间"
        sortable="custom"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="活动名称"
        prop="title">
      </el-table-column>
      <el-table-column
        label="活动说明"
        :show-overflow-tooltip="true"
        prop="content">
      </el-table-column>
      <el-table-column
        label="开始时间"
        sortable="custom"
        prop="strattime">
      </el-table-column>
      <el-table-column
        label="结束时间"
        sortable="custom"
        prop="endtime">
      </el-table-column>
      <el-table-column
        label="鲜花总数"
        width="110px"
        prop="flowercount">
      </el-table-column>
      <el-table-column
        label="剩余鲜花"
        sortable="custom"
        width="110px"
        prop="flower">
      </el-table-column>
      <el-table-column
        label="活动状态"
        prop="status">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog :title="isAdd ? '新增活动' : '信息修改'" :visible.sync="showAdd" width="600px">
      <el-form :model="activity" :rules="rules" label-width="90px" ref="addForm">
        <el-form-item label="活动名称" prop="title">
          <el-input v-model="activity.title"/>
        </el-form-item>
        <el-form-item label="活动说明" prop="content">
          <el-input v-model="activity.content" type="textarea" :autosize="{ minRows: 4}"/>
        </el-form-item>
        <el-form-item label="活动时间" prop="times">
          <el-date-picker
            v-model="activity.times"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发放鲜花" prop="flower" v-if="isAdd">
          <el-input-number v-model="activity.flower" :min="0"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="addActivity" :loading="addLoading">{{ addLoading ? '处理中...' : '提交' }}</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'activity',
  data () {
    return {
      searchItems: [
        {
          label: '创建时间',
          type: 'times',
          key: 'times'
        },
        {
          label: '标题',
          type: 'input',
          key: 'title'
        }
      ], // 检索结构
      activities: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      activity: {
        title: '',
        content: '',
        times: [],
        strattime: '',
        endtime: '',
        flower: 0
      },
      rules: {
        title: [
          { required: true, message: '请输入活动标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入活动内容', trigger: 'blur' }
        ],
        times: [
          { required: true, message: '请选择活动时间', trigger: 'change' }
        ],
        flower: [
          { required: true, message: '请输入鲜花数量', trigger: 'change' }
        ]
      },
      isAdd: true,
      updateRow: null,
      showAdd: false,
      addLoading: false,
      lifting: null
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize, orderby: this.lifting}
      this.axios.get('/Activity/index?isbackstage=0', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.activities = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },
    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.showAdd = true
      this.updateRow = row
      this.$nextTick(() => {
        Object.keys(this.activity).forEach(key => {
          this.activity[key] = row[key]
        })
        this.activity.times = [row.strattime, row.endtime]
        this.$refs['addForm'].clearValidate()
      })
    },
    toDelete (id) {
      this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRow(id)
      })
    },
    deleteRow (id) {
      this.axios.get('/Activity/delete', {params: {id}})
        .then(res => {
          if (res.data.status === 1) {
            this.getList()
            this.$message.success('删除成功')
          }
        })
    },
    addActivity () {
      this.addLoading = true
      let params = {
        content: '',
        endtime: '',
        flower: '',
        strattime: '',
        title: ''
      }
      let url = '/Activity/add'
      Object.keys(params).forEach(key => {
        params[key] = this.activity[key]
      })
      params.strattime = this.activity.times[0]
      params.endtime = this.activity.times[1]
      if (!this.isAdd) {
        url = '/Activity/edit'
        params.id = this.updateRow.id
      }
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.axios.post(url, this.qs.stringify(params))
            .then(res => {
              this.addLoading = false
              if (res.data.status === 1) {
                this.$message.success(res.data.msg)
                this.getList()
                this.showAdd = false
              }
            })
        } else {
          this.$message.error('请检查表单是否填写正确')
          this.addLoading = false
          return false
        }
      })
    },

    orderByTime (lifting) {
      if (lifting.order === 'ascending') {
        this.lifting = lifting.prop + ' asc'
      } else {
        this.lifting = lifting.prop + ' desc'
      }
      this.getList()
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>

</style>
